import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {InitData, List, Brands, Detail, Claim, ClaimDetails} from './DTO';
const baseUrl = '/afterSales/api/v1';
import {baseDataStatus} from '../Enum';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '旧件销毁单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const submitDesc = formatMessage({
    id: 'api.message.submitDesc',
    defaultMessage: '下发{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const passDesc = formatMessage({
    id: 'api.message.passDesc',
    defaultMessage: '审核通过'
});
const exportDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '导出成功'
});
const exportListDesc = formatMessage({
    id: 'api.message.exportListDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});
const rejectDesc = formatMessage({
    id: 'api.message.rejectDesc',
    defaultMessage: '审核不通过'
});

const returnToPrevDesc = formatMessage({
    id: 'api.message.returnToPrevDesc',
    defaultMessage: '审核驳回'
});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const confirmDesc = formatMessage({
    id: 'api.message.confirmDesc',
    defaultMessage: '确认{resourceName}'
}, {resourceName});

const claimDesc = formatMessage({
    id: 'api.message.claimDesc',
    defaultMessage: '查询维修索赔单'
});

const employeeDesc = formatMessage({
    id: 'api.message.employee',
    defaultMessage: '查询服务站雇员'
});

const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});
export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brands)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, brandDesc));
};
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/usedPartDestory/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/usedPartDestories/forCurrentUser?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 作废
export const abandonInfo = (id, data) => request(`${baseUrl}/usedPartDestories/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

// 提交/下发
export const submitInfo = (id, data) => request(`${baseUrl}/usedPartDestories/${id}/submit`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDesc));


// 确认
export const confirmInfo = (id, data) => request(`${baseUrl}/usedPartDestories/${id}/confirm`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, confirmDesc));


// 查询详情
export const getDetail = id => request(`${baseUrl}/usedPartDestories/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));


// 审核通过
export const passInfo = (id, data) => request(`${baseUrl}/usedPartDestories/${id}/pass`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, passDesc));

// 审核不通过
export const rejectInfo = (id, data) => request(`${baseUrl}/usedPartDestories/${id}/reject`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, rejectDesc));

// 审核驳回
export const returnToPrevInfo = (id, data) => request(`${baseUrl}/usedPartDestories/${id}/returnToPrev`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, returnToPrevDesc));

// 新增
export const addInfo = data => request(`${baseUrl}/usedPartDestories`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/usedPartDestories/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 导出
export const exportUsedPartDestory = id => request(`${baseUrl}/usedPartDestories/${id}/export`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, exportDesc));

// 导出列表
export const exportInfo = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/usedPartDestories/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportListDesc))
        .then(res => res.data);
};
//导出列表详情
export const confirmExport = id =>
    request(`${baseUrl}/usedPartDestories/${id}/confirmExport`, null).then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc));

// 查询维修索赔单
export const getClaim = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claims/forUsedPartDestory?${queryString}`, null, Claim)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, claimDesc));
};
export const getClaimDetails = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claims/details?${queryString}`, null, ClaimDetails)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, claimDesc));
};


const getDealerDesc = formatMessage({
    id: 'api.message.getDealerDesc',
    defaultMessage: '查询服务站信息'
});

// 查询服务站
export const getDealers = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forServiceInformation?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};


//根据条件查询总代子公司
export const getDealerSimple = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dealers/forServiceInformation/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDealerDesc));
};

export const getClaimDetailAlls = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/claims/forUsedPartDestory/all?${queryString}`, null, ClaimDetails)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, claimDesc));
};
